<?php
 
 class UploadAction extends AdminAction{

 	#图片上传
 	public function image(){
 		$this->display();
 	}

 	#图片上传
 	public function singleImage(){
 		$this->display();
 	}

 	#文件上传
 	public function file(){
 		$this->display();
 	}
	
	#保存图片
	public function saveimage(){
		if($_POST){
			echo $this->_upload();
		}
	}

	public function savefile(){
		if($_POST){
			$resault = $this->_savefile();
			if($resault['state']=='SUCCESS'){
				$this->assign('resault',$resault);
				$this->display();
			}else{
				echo $resault['state'];
			}
		}
	}

	#保存图片
	private function _upload($dir=NULL){
		if (empty($dir)) {
			$path = '.'.C('UPLOAD_PATH');
		}else{
			$path = '.'.C('UPLOAD_PATH').$dir.'/';
		}
		import("ORG.Net.UploadFile");
		$upload=new UploadFile();
		$upload->maxSize = C('image_size')*1000;  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
		$upload->savePath = $path;        //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
		$upload->autoSub = true;
		$upload->subType = 'date';
		$upload->dateFormat = 'Ym';
		$upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
		$upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
		$upload->allowExts= explode(',',C('image_exts'));     //准许上传的文件后缀
		//$upload->allowTypes= C('IMAGE_TYPE');  //检测mime类型		
		if($upload->upload()){

			$info=$upload->getUploadFileInfo();

			if (empty($dir)) {
				$url = $info[0]["savename"];
			}else{
				$url = $dir.'/'.$info[0]["savename"];
			}

			//缩放处理
			if (C('THUMB_IMAGE')) {
				$tempUrl = C('UPLOAD_PATH').$url;
				$tempUrl = substr($tempUrl,1,strlen($tempUrl));
				$im=getimagesize($tempUrl);
				if ($im[0]>C('IMAGE_MAX_WIDTH')) {
					import("@.ORG.Thumb");
					$resizeimage = new Thumb($tempUrl,C('IMAGE_MAX_WIDTH'),1,$tempUrl,-1,0);  //生成宽度660
				}
			}#结束
			

			return json_encode(array(

				'url' => $url,   //保存后的文件路径
		        'state'    => 'SUCCESS'  //上传状态，成功时返回SUCCESS,其他任何值将原样返回至图片上传框中
			));
			
		}else{
			//是专门来获取上传的错误信息的			
			return json_encode(array(
				'state' => $upload->getErrorMsg(),  //上传状态，返回错误结果
			));
		}
	}

	#保存文件
	private function _savefile($dir=NULL){
		if (empty($dir)) {
			$path = '.'.C('UPLOAD_PATH');
		}else{
			$path = '.'.C('UPLOAD_PATH').$dir.'/';
		}
		import("ORG.Net.UploadFile");
		$upload=new UploadFile();
		$upload->maxSize = C('file_size')*1000;  //是指上传文件的大小，默认为-1,不限制上传文件大小bytes
		$upload->savePath = $path;        //上传保存到什么地方？路径建议大家已主文件平级目录或者平级目录的子目录来保存
		$upload->autoSub = true;
		$upload->subType = 'date';
		$upload->dateFormat = 'Ym';
		$upload->saveRule=uniqid;    //上传文件的文件名保存规则  time uniqid  com_create_guid  uniqid
		$upload->uploadReplace=true;     //如果存在同名文件是否进行覆盖
		$upload->allowExts= explode(',',C('file_exts'));     //准许上传的文件后缀
		//$upload->allowTypes= C('IMAGE_TYPE');  //检测mime类型

		if($upload->upload()){
			$info=$upload->getUploadFileInfo();
			if (empty($dir)) {
				$url = $info[0]["savename"];
			}else{
				$url = $dir.'/'.$info[0]["savename"];
			}
			$showsize = $info[0]['size'].'Byte';
			
			$size = $info[0]['size'];
			if($size > 1024){
				$size = round($size/1024,2);
				$showsize = $size . 'KB';
			}

			if($size > 1024){
				$size = round($size/1024,2);
				$showsize = $size . 'MB';
			}

			if($size > 1024){
				$size = round($size/1024,2);
				$showsize = $size . 'GB';
			}
			
			return array('state'=>'SUCCESS','url'=>$url,'size'=>$showsize);

		}else{
			//是专门来获取上传的错误信息的			
			return array('state'=>$upload->getErrorMsg());
		}
	}
 }

?>